package com.zone.vehicle.sdk.module.superapp.vehicle.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

import com.zone.soa.digitalzonestore.DigitalZoneStoreProxy;
import com.zone.soa.digitalzonestore.DigitalZoneStoreProxyListener;
import com.zone.soa.digitalzonestore.datatypes.AppDownloadInfoStruct;
import com.zone.vehicle.sdk.module.superapp.vehicle.IVehicleService;
import com.zone.vehicle.sdk.module.superapp.vehicle.listener.IListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class DigitalZoneStoreProxyService implements IVehicleService {

    private static final String TAG = DigitalZoneStoreProxyService.class.getSimpleName();

    public static final String INSTALL_INTENT_ACTION="com.shangqi.digitalstorecar.install.action";
    public static final String INSTALL_INTENT_EXTRA="installer_code";

    /*
     * icm
     * */
    Timer timer = new Timer();
    private static final int DELAY = 1000;

    DigitalZoneStoreProxy m_proxy;
    @Override
    public void startService() {
        m_proxy = new DigitalZoneStoreProxy("/vendor/etc/sovp/com.shangqi.digitalstorecar");
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Log.e(TAG, "run:startFindService");
                int result = m_proxy.startFindService(DigitalZoneStoreProxy.INSTANCE_NAME_1);
                Log.e(TAG, "findStoreService6: result:" + result);
                if (result != 0) {
                    m_proxy.localAreaCheckResult(1, 2);
                }
            }
        }, DELAY, DELAY);
    }

    @Override
    public void registerListener(IListener listener) {
        m_proxy.registerListener(new DigitalZoneStoreProxyListener() {
            @Override
            public void onNotifyServiceAvailable(boolean isAvailable) {
                Log.e(TAG, "==========onNotifyServiceAvailable isAvailable: " + isAvailable);
                if (isAvailable) {
                    List<String> subEventList = new ArrayList<>();
                    subEventList.add(DigitalZoneStoreProxy.APPDOWNLOADINFO);
                    subEventList.add(DigitalZoneStoreProxy.LOCALSTOREAREACHECK);
                    Log.d(TAG, "==========subscribeEvent==========");
                    m_proxy.subscribeEvent(subEventList);
                }
            }

            @Override
            public void onNotifyServiceDied(String serviceName) {

            }

            @Override
            public void onNotifyAppDownloadInfo(AppDownloadInfoStruct appDownloadInfo) {
                Log.e(TAG, "onNotifyAppDownloadInfo: appinfo1:" + appDownloadInfo.getDownloadURL());
                Log.e(TAG, "onNotifyAppDownloadInfo: appinfo2:" + appDownloadInfo.getConsumerName());
//                DownloadUrlBean downloadUrlBean = GsonUtils.fromJson(appDownloadInfo.getDownloadURL(), DownloadUrlBean.class);
//                if (downloadUrlBean.getDownloadUrl() != null) {
//                    downloadApk(downloadUrlBean, context);
//                    Log.e(TAG, "start downloadAPk()");
//                } else {
//                    Log.e(TAG, "ICM ERR OR JSON TO BEAN ERR");
//                }
            }
        });
    }

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction() == INSTALL_INTENT_ACTION) {
                if (intent.getIntExtra(INSTALL_INTENT_EXTRA, -1) == 1) {
                    Log.e(TAG, "success install");
//                    if (completeDownloadUrlBean != null) {
//                        Log.e(TAG, "success install execute");
//                        execute(completeDownloadUrlBean.getAppCode(), DInteractionRepository.INSTALL, String.valueOf(100), DInteractionRepository.INSTALL_SUCCESSFUL, completeDownloadUrlBean.getUserId());
//                    }
                } else if (intent.getIntExtra(INSTALL_INTENT_EXTRA, -1) == 0) {
                    Log.e(TAG, "fail  install");
//                    if (completeDownloadUrlBean != null) {
//                        Log.e(TAG, "fail install execute");
//                        execute(completeDownloadUrlBean.getAppCode(), DInteractionRepository.INSTALL, String.valueOf(-1), DInteractionRepository.INSTALL_FAIL, completeDownloadUrlBean.getUserId());
//                    }
                } else {
                    Log.e(TAG, "fail intent ");
                }
            }
        }
    };

    //注册广播
    private void registerReceiver(Context context) {
        IntentFilter filter = new IntentFilter();
        filter.addAction(INSTALL_INTENT_ACTION);
        context.registerReceiver(broadcastReceiver, filter);
    }

    //解除注册广播
    private void unregisterReceiver(Context context) {
        context.unregisterReceiver(broadcastReceiver);
    }
}
